﻿@model WebApiUserModel

@{
    var parentModel = ViewData["Parent"] as ConfigurationModel;
    var publicKeyStr = T("Plugins.Api.WebApi.PublicKey");
    var secretKeyStr = T("Plugins.Api.WebApi.SecretKey");
    var apiEnabledStr = T("Plugins.Api.WebApi.ApiEnabled");
    var lastRequestStr = T("Plugins.Api.WebApi.LastRequest");
}

<datagrid id="apiuser-grid" class="h-100"
          allow-resize="true"
          allow-row-selection="true"
          allow-column-reordering="true"
          onrowclass="usersGrid_onRowClass">
    <datasource read="@Url.Action("UserList")" />
    <paging position="Bottom" show-size-chooser="true" />
    <sorting enabled="true" allow-unsort="true" allow-multisort="false"></sorting>
    <toolbar>
        <toolbar-group>
            <button datagrid-action="DataGridToolAction.ToggleSearchPanel" class="btn btn-light btn-icon">
                <i class="fa fa-fw fa-filter"></i>
            </button>
        </toolbar-group>
        <zone name="datagrid_toolbar_alpha"></zone>
        <toolbar-group class="omega"></toolbar-group>
        <zone name="datagrid_toolbar_omega"></zone>
    </toolbar>
    <search-panel>
        <partial name="_Grid.Users.Search" model="parentModel.CustomerSearch" />
    </search-panel>
    <columns>
        <column for="Id" type="string" />
        <column for="Email" hideable="false">
            <display-template>
                <a :href="item.row.EditUrl" class="text-truncate">
                    {{ item.value }}
                </a>
            </display-template>
        </column>
        @if (ViewBag.UsernamesEnabled)
        {
            <column for="Username" />
        }
        <column for="FullName" />
        <column for="Active" halign="center" />
        <column for="AdminComment" />
        <column for="PublicKey" sortable="false" width="2fr">
            <display-template>
                <div class="dg-cell-value">
                    <div v-if="!_.isEmpty(item.row.SecretKey)" class="apiuser-info"@*v-bind:class="{ hide: _.isEmpty(item.row.SecretKey) }"*@>
                        <div class="d-table">
                            <div class="d-table-row">
                                <div class="d-table-cell">@publicKeyStr:&nbsp;</div><div class="d-table-cell apiuser-publickey">{{ item.row.PublicKey }}</div>
                            </div>
                            <div class="d-table-row">
                                <div class="d-table-cell">@secretKeyStr:&nbsp;</div><div class="d-table-cell apiuser-secretkey">{{ item.row.SecretKey }}</div>
                            </div>
                            <div class="d-table-row">
                                <div class="d-table-cell">@apiEnabledStr:&nbsp;</div><div class="d-table-cell apiuser-enabled">{{ item.row.EnabledString }}</div>
                            </div>
                            <div class="d-table-row">
                                <div class="d-table-cell">@lastRequestStr:&nbsp;</div><div class="d-table-cell apiuser-lastrequest">{{ item.row.LastRequestDateString }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </display-template>
        </column>
    </columns>
    <row-commands>
        <a datarow-action="DataRowAction.Custom" href="javascript:;" class="apiuser-grid-cmd enable-user" title="@T("Plugins.Api.WebApi.Activate.Hint")"
           :data-key="item.row.Id"
           data-url="@Url.Action("EnableUser", new { enable = true })"
           v-bind:class="{ disabled: _.isEmpty(item.row.PublicKey) || item.row.Enabled }">
            @T("Plugins.Api.WebApi.Activate")
        </a>
        <a datarow-action="DataRowAction.Custom" href="javascript:;" class="apiuser-grid-cmd enable-user" title="@T("Plugins.Api.WebApi.Deactivate.Hint")"
           :data-key="item.row.Id"
           data-url="@Url.Action("EnableUser", new { enable = false })"
           v-bind:class="{ disabled: !item.row.Enabled }">
            @T("Plugins.Api.WebApi.Deactivate")
        </a>

        <div class="dropdown-divider"></div>

        <a datarow-action="DataRowAction.Custom" href="javascript:;" class="apiuser-grid-cmd create-keys" title="@T("Plugins.Api.WebApi.CreateKeys.Hint")"
           :data-key="item.row.Id"
           data-url="@Url.Action("UserKeys", new { create = true })"
           v-bind:class="{ hide: !_.isEmpty(item.row.PublicKey) }">
            @T("Plugins.Api.WebApi.CreateKeys")
        </a>
        <a datarow-action="DataRowAction.Custom" href="javascript:;" class="apiuser-grid-cmd show-keys" 
           :data-key="item.row.Id"
           data-url="@Url.Action("UserKeys", new { create = false })"
           v-bind:class="{ hide: _.isEmpty(item.row.PublicKey) }">
            @T("Plugins.Api.WebApi.ShowKeys")
        </a>
        <a datarow-action="DataRowAction.Custom" href="javascript:;" class="apiuser-grid-cmd delete-keys" title="@T("Plugins.Api.WebApi.RemoveKeys.Hint")"
           :data-key="item.row.Id"
           data-url="@Url.Action("DeleteUserKeys")"
           v-bind:class="{ disabled: _.isEmpty(item.row.PublicKey) }">
            @T("Plugins.Api.WebApi.RemoveKeys")
        </a>
    </row-commands>
</datagrid>

<script sm-target-zone="scripts" data-origin="webapi-users-grid">
    function usersGrid_onRowClass(row) {
        return {
            "text-muted": !row.Enabled
        };
    }
</script>